/*
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.dspl.validate.validator;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.dspl.model.Dataset;
import com.google.dspl.validate.ItemReference;
import com.google.dspl.validate.ValidationContext;
import com.google.dspl.validate.violation.DatasetNoIdViolation;
import com.google.dspl.validate.violation.Violation;

import java.util.List;

/**
 * Simple {@link Validator} for {@link Dataset}s that only validates the basic structures of the
 * dataset itself, not any of its major pieces, such as concepts, tables, slices or topics.
 *
 * @author James Grimm
 */
public class SimpleDatasetValidator implements Validator<Dataset> {

  @Override
  public List<Violation> validate(Dataset dataset, ValidationContext context) {
    ImmutableList.Builder<Violation> violations = ImmutableList.builder();

    validateId(dataset, context, violations);
    validateInfo(dataset, context, violations);
    return violations.build();
  }

  private void validateId(Dataset dataset, ValidationContext context,
      ImmutableList.Builder<Violation> violations) {
    if (Strings.isNullOrEmpty(dataset.getDatasetId())) {
      violations.add(new DatasetNoIdViolation(ItemReference.reference(dataset)));
    }
  }

  private void validateInfo(Dataset dataset, ValidationContext context,
      ImmutableList.Builder<Violation> violations) {
    InfoValidator validator = new InfoValidator(ItemReference.reference(dataset), true);
    validator.validate(dataset.getInfo(), violations);
  }
}
